package com.yinuo.controller;

import com.alibaba.fastjson.JSON;
import com.yinuo.common.*;
import com.yinuo.entity.GlobalContext;
import com.yinuo.entity.ServiceManage;
import com.yinuo.repository.ServiceManageRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gt.ao on 2018/9/25.
 */
@RestController
@RequestMapping("serviceImages")
public class ImageController {
    private static Logger logger = LoggerFactory.getLogger(ImageController.class);

    @RequestMapping("*")
    public void responseByte(HttpServletRequest request,HttpServletResponse response){
        OutputStream out = null;
        FileInputStream fis = null;
        String filePath = request.getServletPath();
        try {
            String rootPath = SystemWaiter.getFilesRootPath();
            filePath = rootPath+GlobalContext.SERVICE_PATH+filePath.substring(filePath.lastIndexOf("/"));
            fis = new FileInputStream(filePath);
            out = response.getOutputStream();
            byte[] b = new byte[2048];
            int len = 0;
            while((len=fis.read(b)) > 0){
                out.write(b,0,len);
            }
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(out!=null){
                    out.close();
                }
                if(fis!=null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
